part of pda.page;

class PageCheck extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PageCheckState();
  }
}

class _PageCheckState extends State<PageCheck> {
  TextEditingController searchController = TextEditingController();
  TextEditingController checkDateController;
  String checkDepart;

  @override
  void initState() {
    checkDateController =
        TextEditingController(text: formatDateOnly(DateTime.now()));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: AppBar(
          title: Text('盘点'),
        ),
        body: Column(
//          child: Text('check'),
          children: <Widget>[
            searchField(),
            billList(),
          ],
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            IconButton(
              iconSize: 36,
              icon: Icon(Icons.add_circle_outline),
              onPressed: () {
                create();
              },
            ),
            IconButton(
              iconSize: 36,
              icon: Icon(Icons.delete_forever),
              onPressed: () {
                delete();
              },
            )
          ],
        ),
      ),
      onWillPop: () async {
        return true;
      },
    );
  }

  Widget searchField() {
    return Row(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(right: 8),
        ),
        Expanded(
          child: TextField(
            controller: searchController,
            decoration: InputDecoration(
//                contentPadding: EdgeInsets.all(0),
                hintText: '输入单号来搜索',
                suffixIcon: IconButton(
                  icon: Icon(Icons.remove_circle),
                  onPressed: () {
                    searchController.clear();
                  },
                )),
          ),
        ),
//        Container(padding: EdgeInsets.only(left: 8.0), child: Text('用户:')),
        IconButton(
          padding: EdgeInsets.only(top: 8.0),
          icon: Icon(Icons.search),
          onPressed: () {
            search();
          },
        )
      ],
    );
  }

  Widget billList() {
    return Expanded(
      child: ListView(
        padding: EdgeInsets.symmetric(horizontal: 0),
        children: <Widget>[
          ListTile(
            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
            leading: Checkbox(
                value: true,
                tristate: true,
                onChanged: (checked) {
                  checked = !checked;
                  return checked;
                }),
            title: Text('单号11234567890'),
            subtitle: Text('部门1/盘点日期1'),
//            selected: true,
            trailing: IconButton(
                icon: Icon(Icons.arrow_forward_ios), onPressed: () {}),
          ),
          Divider(),
          ListTile(
            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
            leading: Checkbox(
                value: true,
                tristate: true,
                onChanged: (checked) {
                  checked = !checked;
                  return checked;
                }),
            title: Text('单号2'),
            subtitle: Text('部门2/盘点日期2'),
//            selected: true,
            trailing: IconButton(
                icon: Icon(Icons.arrow_forward_ios),
                onPressed: () {
                  view();
                }),
          ),
          Divider(),
        ],
      ),
    );
  }

  search() {}

  view() {
    Navigator.popUntil(context, (router) {
      return router.settings.name == '/check';
    });
    Navigator.push(context, MaterialPageRoute(builder: (_) {
      return PageCheckDetail(CheckBill(
        checkDate: parseDateOnly(checkDateController.text),
        depart: checkDepart,
      ));
    }));
  }

  create() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text('选择盘点日期和仓库'),
            titlePadding: EdgeInsets.only(top: 16.0, left: 16.0),
            contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 16),
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('日期:'),
                  Expanded(
                    child: TextField(
                      textAlign: TextAlign.center,
                      controller: checkDateController,
                      enabled: false,
                    ),
                  ),
                  IconButton(
                    padding: EdgeInsets.only(left: 12),
                    iconSize: 24,
                    icon: Icon(
                      Icons.date_range,
                    ),
                    onPressed: () {
                      showDatePicker(
                              context: context,
//                              locale: Locale('en', 'US'),
                              initialDate:
                                  parseDateOnly(checkDateController.text),
                              firstDate: DateTime(2000),
                              lastDate: DateTime.now().add(Duration(days: 100)))
                          .then((date) {
                        setState(() {
                          checkDateController.text = formatDateOnly(date);
                        });
                      });
                    },
                  )
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
//                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  Text('仓库:'),
                  Expanded(child: getDepartSelector()),
                ],
              ),
              FlatButton(
                textColor: Colors.blue,
                child: Text('确定'),
                onPressed: () {
                  view();
                },
              )
            ],
          );
        });
  }

  Widget getDepartSelector() {
    return FutureBuilder(
      future: Depart.getAll(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List<Depart> departs = snapshot.data;
          List<DropdownMenuItem> items = [];
          departs.forEach((depart) {
            items.add(DropdownMenuItem(
//              child: Text(
//                depart.name,
//                textAlign: TextAlign.right,
//              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[Text(depart.name)],
              ),
              value: depart.id,
            ));
          });
          checkDepart = departs.first.id;
          var dropDown = DropdownButton(
            items: items,
            hint: Text('请选择部门'),
            value: checkDepart,
            onChanged: (selected) {
              //下拉菜单item点击之后的回调
              setState(() {
                checkDepart = selected;
              });
            },
//            isDense: false,
            //减少按钮的高度。默认情况下，此按钮的高度与其菜单项的高度相同。如果isDense为true，则按钮的高度减少约一半。 这个当按钮嵌入添加的容器中时，非常有用
            iconSize: 32.0,
            isExpanded: true,
          );
          return dropDown;
        } else {
          return Text('...');
        }
      },
    );
  }

  delete() {}
}
